package com.xjscrm.server.biz.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.server.biz.mq.producer.SendMsgProducer;
import com.xjscrm.server.common.DelayTopics;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 机器人延迟队列消费
 *
 * @author JiangHam
 * @date 2024-02-29 16:41
 */
@Slf4j
@RocketMQMessageListener(topic = DelayTopics.WW_TRIGGER_TOPIC, consumerGroup = DelayTopics.WW_TRIGGER_TOPIC)
@Component
public class DelayTriggerTopicConsumer extends BaseTriggerMqConsumer {

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Override
    public void doMessageHandle(MicaTriggerDto micaTriggerDto) {

        try {
            log.info("--收到发往机器人的延迟队列消息={}", JSONObject.toJSONString(micaTriggerDto));
            MicaTriggerType byValue = MicaTriggerType.getByValue(micaTriggerDto.getMsgType());
            if (micaTriggerDto.getDelay() > 0) {//还在延迟中，继续丢入延时队列当中
                log.info("--延迟处理发往机器人的延迟队列消息{}，delay={}, micaTransDto={}", byValue.getDesc(), micaTriggerDto.getDelay(), JSONObject.toJSONString(micaTriggerDto));
                sendMsgProducer.pushMsgDelay(micaTriggerDto, DelayTopics.WW_TRIGGER_TOPIC);
            } else { //立即消费，立即丢入触发指令主题中
                log.info("--立即处理发往机器人的延迟队列消息{}，delay={}, micaTransDto={}", byValue.getDesc(), micaTriggerDto.getDelay(), JSONObject.toJSONString(micaTriggerDto));
                sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }


    }
}
